import UserService from '../../../service/UserService';
import CommonService from '../../../service/CommonService'
import { throttle } from '../../../utils/util.js';

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    errMsg: '',
    user: {},
    validDisabled: false,
    validTxt: '获取验证码'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.userService = new UserService();
    this.commonService= new CommonService();
    this.setData({ user: app.globalData.user});
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const { chooseCity } = app.globalData;
    const { user } = this.data;
    if (chooseCity.id){
      user.city = chooseCity;
      user.adcode = chooseCity.id;
      this.setData({ user });
    }
    
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    app.globalData.chooseCity = {};
  },
  validForm: function(form){
    if (!form.phone) {
      this.setData({ errMsg: '手机号码不能为空' })
      return false;
    }
    if (!form.cardFrontImg) {
      this.setData({ errMsg: '请上传身份证正面照' })
      return false;
    }
    if (!form.cardBackImg) {
      this.setData({ errMsg: '请上传身份证背面照' })
      return false;
    }
    if (!form.handCardFrontImg) {
      this.setData({ errMsg: '请上传手持身份证正面照' })
      return false;
    }
    if (!form.handCardBackImg) {
      this.setData({ errMsg: '请上传手持身份证背面照' })
      return false;
    }
    this.setData({ errMsg: '' });
    return true;
  },
  formSubmit: function(e){
    const {user} = this.data;
    if (!this.validForm(user)){
      return;
    }
    const param = {
      id: user.id,
      faceUrl: user.faceUrl,
      phone: user.phone,
      userType: 1,
      validCode: user.validCode,
      trueName: user.trueName,
      sex: user.sex,
      userCard: user.userCard,
      adcode: user.adcode,
      lat: user.lat,
      lng: user.lng,
      address: user.address,
      cardFrontImg: user.cardFrontImg,
      cardBackImg: user.cardBackImg,
      handCardFrontImg: user.handCardFrontImg,
      handCardBackImg: user.handCardBackImg,
      bankName: user.bankName,
      bankUser: user.bankUser,
      bankNum: user.bankNum,
      bankAddr: user.bankAddr
    }
    this.userService.authApply(param).then(function(res){
      if (res.status == 0) {
        app.globalData.user.userType = 1;
        app.globalData.user.authStatus = 1;
        wx.navigateTo({
          url: '/packageUser/pages/auth/auditting?authStatus=1'
        })
      }
    });
  },
  setFieldValue:function(field, value){
    const { user } = this.data;
    user[field] = value;
    this.setData({ user });
  },
  onFaceChange:function(e){
    this.setFieldValue('faceUrl', e.detail.value);
  },
  onTrueNameChange: function(e){
    this.setFieldValue('trueName', e.detail.value);
  },
  onPhoneChange: function(e){
    this.setFieldValue('phone', e.detail.value);
  },
  onCode: function (e) {
    this.setFieldValue('validCode', e.detail.value);
  },
  onSexChange: function(e){
    this.setFieldValue('sex', e.detail.value);
  },
  onUserCardChange: function (e) {
    this.setFieldValue('userCard', e.detail.value);
  },
  onAddressChange: function (e) {
    this.setFieldValue('address', e.detail.value);
  },
  onCardFront: function (e) {
    this.setFieldValue('cardFrontImg', e.detail.value);
  },
  onCardBack: function (e) {
    this.setFieldValue('cardBackImg', e.detail.value);
  },
  onHandCardFront: function (e) {
    this.setFieldValue('handCardFrontImg', e.detail.value);
  },
  onHandCardBack: function (e) {
    this.setFieldValue('handCardBackImg', e.detail.value);
  },
  onBankNameChange: function(e){
    this.setFieldValue('bankName', e.detail.value);
  },
  onBankUserChange: function (e) {
    this.setFieldValue('bankUser', e.detail.value);
  },
  onBankNumChange: function (e) {
    this.setFieldValue('bankNum', e.detail.value);
  },
  onBankAddrChange: function (e) {
    this.setFieldValue('bankAddr', e.detail.value);
  },
  /**获取短信验证码 */
  onValidCode: function(e){
    const _ = this;
    const { user } = this.data;
    if(!user.phone){
      this.setData({ errMsg: '手机号码不能为空' })
      return;
    }
    this.setData({ validDisabled: true, errMsg:'' })
    this.commonService.getValidCode(user.phone).then(res=>{
      console.log(res);
      if(res.status == 0){
        let count = 60;
        const t = setInterval(()=>{
          const {validTxt} = _.data;
          count--;
          _.setData({ validTxt: count });
          if (count < 0){
            _.setData({ validTxt: '重新获取', validDisabled: false });
            clearInterval(t);
          }
        }, 1000);
      }
      
    })
  },
  
  onLocationTap: function () {
    throttle(function (e) {
      wx.navigateTo({
        url: '/pages/citys/citys'
      })
    }, 1000)();
  },
  onLocation: function (e) {
    const _ = this;
    const { user } = this.data;
    wx.chooseLocation({
      success: function (res) {
        user.address = res.address;
        user.lng = res.longitude;
        user.lat = res.latitude;
        _.setData({ user });
      }
    })
  }
})